﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HSDc.RingleFlow.Api.Service;
using HSDc.RingleFlow.Api.Kernel;
using HSDc.RingleFlow.Kernel.ProcessActivity;

namespace HSDc.RingleFlow.Kernel.Default
{
    public class RuntimeServiceImpl: IRuntimeService
    {
        
        IProcessInstance IRuntimeService.CreateProcessInstance(IProcessDefinition definition)
        {
            return CreateInstance(definition, null);
        }

        private ProcessInstance CreateInstance(IProcessDefinition definition, Dictionary<string, object> variables)
        {
            string _id = Guid.NewGuid().ToString();
            ProcessInstance instance = new ProcessInstance(_id, (ProcessDefinition)definition);
            if (variables!=null)
                instance.SetVariables(variables);
            MemoryRuntimeEnvironment.instances.Add(_id, instance);
            return instance;
        }

        IProcessInstance IRuntimeService.CreateProcessInstance(string definitionId)
        {
            DefaultProcessDefinition pd = new DefaultProcessDefinition();
            List<string> values = pd.GetValueByID(definitionId);
            ProcessDefinition _pd = new ProcessDefinition(values[0], values[1], "1", true, 20);
            InitialDefaultActivities(ref _pd);
            return CreateInstance(_pd, null);
        }

        IProcessInstance IRuntimeService.CreateProcessInstance(IProcessDefinition definition, Dictionary<string, object> variables)
        {
            return CreateInstance(definition, variables);
        }

        IProcessInstance IRuntimeService.CreateProcessInstance(string definitionId, Dictionary<string, object> variables)
        {
            DefaultProcessDefinition pd = new DefaultProcessDefinition();
            List<string> values = pd.GetValueByID(definitionId);
            ProcessDefinition _pd = new ProcessDefinition(values[0], values[1], "1", true, 20);
            InitialDefaultActivities(ref _pd);
            return CreateInstance(_pd, variables);
        }

        private void InitialDefaultActivities(ref ProcessDefinition pd)
        {
            Activity ac, ac2 = null;
            string _id = Guid.NewGuid().ToString();
            ac = new HumanActivity(_id, "主管審核", null, null);
            pd.AddActivity(ac);
            string _id2 = Guid.NewGuid().ToString();
            ac2 = new EndActivity(_id2, "End", null, null);
            Transition trans = new Transition(ac, ac2, null);
            ac.AddOutboundTransition(trans);
            pd.AddActivity(ac);
        }

        Dictionary<string, object> IRuntimeService.GetVariables(string instanceId)
        {
            ProcessInstance instance = null;
            MemoryRuntimeEnvironment.instances.TryGetValue(instanceId, out instance);
            return ((IProcessInstance)instance).GetVaraibles();
        }

        object IRuntimeService.GetVaraible(string instanceId, string variableName)
        {
            ProcessInstance instance = null;
            MemoryRuntimeEnvironment.instances.TryGetValue(instanceId, out instance);
            return ((IProcessInstance)instance).GetVaraible(variableName);
        }

        IProcessInstance IRuntimeService.GetProcessInstance(string instanceId)
        {
            ProcessInstance instance = null;
            MemoryRuntimeEnvironment.instances.TryGetValue(instanceId, out instance);
            return instance;
        }

        void IRuntimeService.CompleteProcessInstance(IProcessInstance instance)
        {
            MemoryRuntimeEnvironment.instances.Remove(instance.Id);
        }


        bool IRuntimeService.IsComplete(string instanceId)
        {
            throw new NotImplementedException();
        }
    }
}
